//
//  ST_AppDelegate.swift
//  BTVideo
//
//  Created by DouLei on 2019/7/8.
//  Copyright © 2019 DouLei. All rights reserved.
//

import UIKit
import NIMSDK
import SwiftyJSON
import SwiftyUserDefaults
import SweeterSwift

func print(_ items: Any..., separator: String = " ", terminator: String = "\n") {
    
    #if DEBUG
        let output = items.map { "\($0)" }.joined(separator: separator)
        
        Swift.print(output, terminator: terminator)
    
        ST_TIANLogPublisher.default.push(log: output)
    #endif
}



class ST_AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        print("App Lauch Finished ")

        #if !DEBUG
        checkInject();
        
        if UIDevice.current.is_jail_broken{
            exit(0);
        }
        #endif
        
        
        
        window = UIWindow.init(frame: UIScreen.main.bounds)
        
        ST_DTBootManager.shared.normal_run()
        
        return true
    }
    
    
//    MARK: - WeChat
    func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
        
        WXApi.handleOpen(url, delegate:ST_DTBootManager.shared.wxHandler)
        
        QQApiInterface.handleOpen(url, delegate: ST_DTBootManager.shared.qqHandler)
        
        TencentOAuth.handleOpen(url)
        
        return true
    }

    
    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {

        WXApi.handleOpen(url, delegate:ST_DTBootManager.shared.wxHandler)
        
        QQApiInterface.handleOpen(url, delegate: ST_DTBootManager.shared.qqHandler)
        
        TencentOAuth.handleOpen(url)
        
        return true
    }
    
    func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
        
        if let url  = userActivity.webpageURL{
            
            QQApiInterface.handleOpenUniversallink(url, delegate: ST_DTBootManager.shared.qqHandler)
            
            TencentOAuth.handleUniversalLink(url)
        }
        
        WXApi.handleOpenUniversalLink(userActivity, delegate: ST_DTBootManager.shared.wxHandler)
        
        
        
        return true
    }
    
    
    func application_will_resign_active(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func application_did_enter_background(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of application_will_terminate: when the user quits.
    }

    func application_will_enter_foreground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func application_did_become_active(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
        
        UIApplication.shared.applicationIconBadgeNumber = 0
        if SC.need_check_o_t_n != nil {
            NotificationCenter.default.post(name: NSNotification.Name.refreshStatusWhenBackApp, object: nil)
        }else {
            NotificationCenter.default.post(name: NSNotification.Name.refreshTicketStatusWhenBackApp, object: nil)
        }
    }

    func application_will_terminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also application_did_enter_background:.
    }
    
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        
        
        NIMSDK.shared().updateApnsToken(deviceToken)
    }
    
    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        
        
        print("不允许推送:",error)
        
    }


}
